package com.z.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.z.common.aop.annotation.Log;
import com.z.common.aop.enums.BusinessType;
import com.z.common.aop.enums.LoggingType;
import com.z.sys.entity.SysLog;
import com.z.sys.service.SysLogService;
import com.z.common.web.base.BaseController;
import com.z.common.web.vo.request.PageVo;
import com.z.common.web.vo.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * Describe: 日志控制器
 */
@RestController
@Api(tags = {"系统日志"})
@RequestMapping("system/log")
public class SystemLogController extends BaseController {

    @Resource
    private SysLogService sysLogService;

    /**
     * Describe: 行为日志视图
     * Param: null
     * Return: ModelAndView
     */
    @GetMapping("main")
    @PreAuthorize("hasPermission('/system/log/main','sys:log:main')")
    public ModelAndView main() {
        return jumpPage("system/log/main");
    }

    /**
     * Describe: 行为日志视图
     * Param: null
     * Return: ModelAndView
     */
    @GetMapping("main_v1")
    @PreAuthorize("hasPermission('/system/log/main','sys:log:main')")
    public ModelAndView main_v1() {
        return jumpPage("system/log/main_v1");
    }

    /**
     * Describe: 操作日志数据
     * Param: null
     * Return: ResultTable
     */
    @GetMapping("operateLog")
    @PreAuthorize("hasPermission('/system/log/operateLog','sys:log:operateLog')")
    public Result operateLog(PageVo pageDomain, LocalDateTime startTime, LocalDateTime endTime) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        PageInfo<SysLog> pageInfo = new PageInfo<>(sysLogService.data(LoggingType.OPERATE, startTime, endTime));
        return Result.table(pageInfo);
    }

    /**
     * 查询日志列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/system/log/data','sys:log:main')")
    @Log(title = "日志", describe = "查询日志列表", type = BusinessType.QUERY)
    @ApiOperation(value = "查询接口")
    public Result list(@ModelAttribute SysLog sysLog, PageVo pageVo) {
        PageInfo<SysLog> pageInfo = sysLogService.selectSysLogPage(sysLog, pageVo);
        return Result.table(pageInfo);
    }

    /**
     * Describe: 登录日志数据
     * Param: null
     * Return: ModelAndView
     */
    @GetMapping("loginLog")
    @PreAuthorize("hasPermission('/system/log/loginLog','sys:log:loginLog')")
    public Result loginLog(PageVo pageDomain, LocalDateTime startTime, LocalDateTime endTime) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        PageInfo<SysLog> pageInfo = new PageInfo<>(sysLogService.data(LoggingType.LOGIN, startTime, endTime));
        return Result.table(pageInfo);
    }

    /**
     * Describe: 日志详情
     * Param: null
     * Return: ModelAndView
     */
    @GetMapping("/info")
    @PreAuthorize("hasPermission('/system/log/info','sys:log:info')")
    public ModelAndView details() {
        return jumpPage("system/log/info");
    }

}
